import { createRouter, createWebHistory } from 'vue-router'
import { authService } from '@/service/AuthService';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    //重定向
    {path:'/', redirect:'/login'},
    // {path: '/home',  meta:{title:'主页'},component: () => import('../views/Home.vue'),},

      {path: '/login', name:"login", meta:{title:'登陆'},component: () => import('../views/Login.vue'),},
    {path: '/index', name:"index", meta:{title:'首页'},component: () => import('../views/Index.vue'),},
    {path: '/businessList',  meta:{title:'商家列表'},component: () => import('../views/BusinessList.vue'),},
    {path: '/businessInfo',  meta:{title:'商家信息'},component: () => import('../views/BusinessInfo.vue'),},
    {path: '/order',  meta:{title:'订单'},component: () => import('../views/Order.vue'),},
    {path: '/payment',  meta:{title:'支付页面'},component: () => import('../views/Payment.vue'),},
    {path: '/orderlist',  meta:{title:'支付页面'},component: () => import('../views/OrderList.vue'),},
    {path: '/register',  meta:{title:'注册'},component: () => import('../views/Register.vue'),},
    {path: '/userAddress',  meta:{title:'地址'},component: () => import('../views/UserAddress.vue'),},
    {path: '/message',  meta:{title:'消息'},component: () => import('../views/Message.vue'),},



  ]
})
// meta 补充信息
// brforeEach 跳转之前做的一些操作
router.beforeEach((to,from,next)=>{
  document.title = to.meta.title

  const isLogged = JSON.parse(localStorage.getItem('isLogged'))
  if (!isLogged) {
    if (to.path !== '/login' && to.path !== '/register' && to.path !== '/index' && to.path !== '/businessList') {
      next('/login'); // 未登录且尝试访问受限路由，重定向到登录页面2
    } else {
      next(); // 未登录但允许访问的路由，允许导航
    }
  } else {
    next(); // 已登录，允许访问所有路由,必须调用这个函数才能实现跳转
  }
})
export default router
